﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using MSOffice = Microsoft.Office.Core;
using MSOutlook = Microsoft.Office.Interop.Outlook;
using Utility = Microsoft.Sample.Outlook.Utility;



// TODO:  Follow these steps to enable the Ribbon (XML) item:

// 1: Copy the following code block into the ThisAddin, ThisWorkbook, or ThisDocument class.

//  protected override Microsoft.Office.Core.IRibbonExtensibility CreateRibbonExtensibilityObject()
//  {
//      return new OutlookRibbon();
//  }

// 2. Create callback methods in the "Ribbon Callbacks" region of this class to handle user
//    actions, such as clicking a button. Note: if you have exported this Ribbon from the Ribbon designer,
//    move your code from the event handlers to the callback methods and modify the code to work with the
//    Ribbon extensibility (RibbonX) programming model.

// 3. Assign attributes to the control tags in the Ribbon XML file to identify the appropriate callback methods in your code.  

// For more information, see the Ribbon XML documentation in the Visual Studio Tools for Office Help.


namespace Microsoft.Sample.Outlook.CRMIntegration
{
    public partial class ThisApplication
    {
        private OutlookRibbon ribbon;
        protected override object RequestService(Guid serviceGuid)
        {
            if (serviceGuid == typeof(MSOffice.IRibbonExtensibility).GUID)
            {
                if (ribbon == null)
                    ribbon = new OutlookRibbon(Application);
                return ribbon;
            }
            return base.RequestService(serviceGuid);
        }
    }

    [ComVisible(true)]
    public class OutlookRibbon : MSOffice.IRibbonExtensibility
    {
        private MSOutlook.Application Application;

        public OutlookRibbon(MSOutlook.Application outlookApplication)
        {
            Application = outlookApplication;
        }

        #region IRibbonExtensibility Members

        public string GetCustomUI(string ribbonID)
        {
            //ribbonID indicates type of Inspector that is about to be displayed
            //Valid ribbonID values are as follows:
            //Microsoft.Outlook.Mail.Read
            //Microsoft.Outlook.Mail.Compose
            //Microsoft.Outlook.MeetingRequest.Read
            //Microsoft.Outlook.MeetingRequest.Send
            //Microsoft.Outlook.Appointment
            //Microsoft.Outlook.Contact
            //Microsoft.Outlook.Journal
            //Microsoft.Outlook.Task
            //Microsoft.Outlook.DistributionList
            //Microsoft.Outlook.Report
            //Microsoft.Outlook.Resend
            //Microsoft.Outlook.Response.Read
            //Microsoft.Outlook.Response.Compose
            //Microsoft.Outlook.Response.CounterPropose
            //Microsoft.Outlook.RSS
            //Microsoft.Outlook.Post.Read
            //Microsoft.Outlook.Post.Compose
            //Microsoft.Outlook.Sharing.Read
            //Microsoft.Outlook.Sharing.Compose
            if (ribbonID == "Microsoft.Outlook.Contact")
            {
                return GetResourceText("Microsoft.Sample.Outlook.CRMIntegration.OutlookRibbon.xml");
            }
            else
            {
                return string.Empty;
            }
        }

        #endregion

        #region Ribbon Callbacks
        //Create callback methods here. For more information about adding callback methods, select the Ribbon XML item in Solution Explorer and then press F1

        public void ColorButton_Action(MSOffice.IRibbonControl control)
        {
            try
            {
                Debug.WriteLine("ColorButton_Action");
                Utility.Ribbon.OutlookInspector window =
                    Utility.CustomItemManager.FindOutlookInspector(control.Context);
                if (window != null)
                {
                    MessageBox.Show("The currently selected color is " +
                        window.RibbonColor,
                        "Color Widgets",
                        MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
        }

        public string ColorButton_GetLabel(MSOffice.IRibbonControl control)
        {
            return "Color";
        }

        public string ColorButton_GetSupertip(MSOffice.IRibbonControl control)
        {
            return "Display selected color";
        }

        public void ColorCombo_OnChange(MSOffice.IRibbonControl control, string text)
        {
            try
            {
                Debug.WriteLine("ColorCombo_OnChange");
                Utility.Ribbon.OutlookInspector window =
                    Utility.CustomItemManager.FindOutlookInspector(control.Context);
                if (window != null)
                {
                    window.RibbonColor = text;
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
        }

        public string ColorCombo_GetText(MSOffice.IRibbonControl control)
        {
            Debug.WriteLine("ColorCombo_GetText");
            Utility.Ribbon.OutlookInspector window =
                Utility.CustomItemManager.FindOutlookInspector(control.Context);
            if (window != null)
            {
                return window.RibbonColor;
            }
            else
            {
                return "Red";
            }
        }

        public int ColorCombo_GetItemCount(MSOffice.IRibbonControl control)
        {
            return 3;
        }

        public string ColorCombo_GetItemLabel(MSOffice.IRibbonControl control, int index)
        {
            //index is 0-based
            switch (index)
            {
                case 0:
                    return "Red";
                case 1:
                    return "Green";
                case 2:
                    return "Blue";
                default:
                    return String.Empty;
            }
        }

        public bool ColorWidgetsGroup_GetVisible(MSOffice.IRibbonControl control)
        {
            Debug.WriteLine("ColorWidgetsGroup_GetVisible");
            Utility.Ribbon.OutlookInspector window =
                Utility.CustomItemManager.FindOutlookInspector(control.Context);
            if (window != null)
            {
                MSOutlook.ContactItem contact = (MSOutlook.ContactItem)window.CurrentItem;
                //Make the group visible only if an address exists
                if (String.IsNullOrEmpty(contact.BusinessAddress) &
                        String.IsNullOrEmpty(contact.HomeAddress) &
                        String.IsNullOrEmpty(contact.OtherAddress))
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            return false;
        }

        public string ColorWidgetsGroup_GetLabel(MSOffice.IRibbonControl control)
        {
            return "Color Widgets";
        }

        public void Ribbon_Load(MSOffice.IRibbonUI ribbonUI)
        {
            ThisApplication._ribbon = ribbonUI;
        }

        #endregion

        #region Helpers

        private static string GetResourceText(string resourceName)
        {
            Assembly asm = Assembly.GetExecutingAssembly();
            string[] resourceNames = asm.GetManifestResourceNames();
            for (int i = 0; i < resourceNames.Length; ++i)
            {
                if (string.Compare(resourceName, resourceNames[i], StringComparison.OrdinalIgnoreCase) == 0)
                {
                    using (StreamReader resourceReader = new StreamReader(asm.GetManifestResourceStream(resourceNames[i])))
                    {
                        if (resourceReader != null)
                        {
                            return resourceReader.ReadToEnd();
                        }
                    }
                }
            }
            return null;
        }

        #endregion
    }
}
